package com.yunyi.lam.demo;

import com.yunyi.lam.pojo.Author;
import com.yunyi.lam.utils.GetAuthors;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 中间操作测试
 *
 * @author yunyi
 */
public class MiddleLearn {
    public static void main(String[] args) {
        List<Author> authors = GetAuthors.getAuthors();
        //testFilter(authors);
        //testMap(authors);
        //testMapAdvance(authors);
        //testSorted01(authors);
        //testSorted02(authors);
        //testLimit(authors);
        //testSkip(authors);
        //testFlatMap01(authors);
        testFlatMapAdvance(authors);
        System.out.println(authors);
    }

    /**
     * FlatMap测试类02
     *
     * @param authors 参数
     */
    private static void testFlatMapAdvance(List<Author> authors) {
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(category -> System.out.println(category));
    }

    /**
     * FlatMap测试类01
     *
     * @param authors 参数
     */
    private static void testFlatMap01(List<Author> authors) {
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(book -> System.out.println(book.getCategory()));
    }

    /**
     * Skip测试类
     *
     * @param authors 参数
     */
    private static void testSkip(List<Author> authors) {
        authors.stream().distinct().sorted().skip(1).forEach(author -> System.out.println(author.getName()));
    }

    /**
     * limit测试类
     *
     * @param authors 参数
     */
    private static void testLimit(List<Author> authors) {
        authors.stream().distinct().sorted().limit(1).forEach(author -> System.out.println(author));
    }

    /**
     * Sorted测试类
     * 有实现的接口
     *
     * @param authors params
     */
    private static void testSorted01(List<Author> authors) {
        authors.stream().sorted().forEach(author -> System.out.println(author));
    }

    /**
     * Sorted测试类
     * 需在现在实现
     *
     * @param authors params
     */
    private static void testSorted02(List<Author> authors) {
        authors.stream().sorted(Comparator.comparingInt(Author::getAge)).forEach(author -> System.out.println(author));
        //authors.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).forEach(author -> System.out.println(author));
    }

    /**
     * map测试类
     *
     * @param authors params
     */
    private static void testMap(List<Author> authors) {
        authors.stream().map(author -> author.getIntro()).forEach(str -> System.out.println(str));
    }

    /**
     * map测试进阶类
     *
     * @param authors params
     */
    private static void testMapAdvance(List<Author> authors) {
        authors.stream().map(author -> author.getIntro() + "2223333").forEach(str -> System.out.println(str));
    }

    /**
     * filter测试类
     *
     * @param authors params
     */
    private static void testFilter(List<Author> authors) {
        authors.stream().filter(author -> author.getAge() > 34).forEach(author -> author.setName(author.getName() + "-" + author.getAge()));
    }
}
